package fun.fykj.liu.interceptor;

import fun.fykj.liu.enums.ResCodeEnum;
import fun.fykj.liu.model.Result;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @version V3.0
 * @Title: GlobalExceptionInterceptor
 * @Company: 成都影达科技有限公司
 * @Description: 描述
 * @author: 东进
 * @date 2019/8/6 上午10:24
 */
@RestControllerAdvice("fun.fanyu.attendance.controller")
public class GlobalExceptionInterceptor {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handleException(Exception e) {
        //异常处理
        if (e instanceof BusinessException) {
            return Result.build(((BusinessException) e).getStatus(), ((BusinessException) e).getMsg());
        } else if (e instanceof DuplicateKeyException) {
            return Result.build(ResCodeEnum.SAVE_FAIL.getStatus(), "请勿重复添加");
        } else {
            e.printStackTrace();
            return Result.build(ResCodeEnum.ERROR.getStatus(), ResCodeEnum.ERROR.getMsg());
        }
    }
}
